package controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import pojo.User;
import service.UserService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin(origins = "*")
@RequestMapping("/user")
@Api(tags = "用户管理接口", description = "提供用户相关的增删改查功能，涵盖用户信息的查询、新增、修改和删除操作")
public class UserController {

    @Autowired
    private UserService userService;

    // 查询所有用户方法
    @GetMapping("/findAll")
    @ResponseBody
    @ApiOperation(value = "查询所有用户", notes = "获取系统中存储的所有用户的详细信息，返回用户对象列表")
    public List<User> findAll() {
        return userService.findAll();
    }

    // 根据 ID 查询用户方法
    @GetMapping("/findById")
    @ResponseBody
    @ApiOperation(value = "根据ID查询用户", notes = "通过传入的用户ID，精准获取该用户在系统中的具体信息")
    @ApiImplicitParam(name = "userId", value = "用户ID，用于唯一标识要查询的用户", required = true, dataType = "Integer", paramType = "query")
    public User findById(@RequestParam Integer userId) {
        return userService.findById(userId);
    }

    // 添加用户方法
    @PostMapping("/add")
    @ResponseBody
    @ApiOperation(value = "添加用户", notes = "将新的用户信息传入系统并进行新增操作，新增成功返回影响行数；若用户名已存在，新增失败")
    public Map<String, Object> add(@RequestBody User user) {
        Map<String, Object> result = new HashMap<>();
        // 调用 service 层校验用户名唯一性
        boolean isUserNameExist = userService.checkUserNameExists(user.getUserName());
        if (isUserNameExist) {
            result.put("code", 400);
            result.put("message", "用户名已存在，请更换用户名重试");
            return result;
        }
        int row = userService.add(user);
        if (row > 0) {
            result.put("code", 200);
            result.put("message", "用户添加成功");
        } else {
            result.put("code", 500);
            result.put("message", "用户添加失败，请检查数据");
        }
        return result;
    }

    // 更新用户方法
    @PostMapping("/update")
    @ResponseBody
    @ApiOperation(value = "更新用户信息", notes = "依据传入的用户对象（含用户ID），对系统中该用户的信息进行更新")
    public int update(@RequestBody User user) {
        return userService.update(user);
    }

    // 删除用户方法
    @PostMapping("/delete")
    @ResponseBody
    @ApiOperation(value = "删除用户", notes = "根据传入的用户ID，从系统中删除对应的用户信息")
    @ApiImplicitParam(name = "userId", value = "用户ID，用于指定要删除的用户", required = true, dataType = "Integer", paramType = "query")
    public int delete(@RequestParam Integer userId) {
        return userService.deleteUser(userId);
    }

    // 登录接口
    @PostMapping("/login")
    @ResponseBody
    @ApiOperation(value = "用户登录", notes = "根据用户名和密码进行登录校验，返回登录结果及用户角色信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "用户名", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String", paramType = "query")
    })
    public Map<String, Object> login(
            @RequestParam String userName,
            @RequestParam String password
    ) {
        Map<String, Object> result = new HashMap<>();
        // 调用 service 层根据用户名查询用户
        User user = userService.findByUserName(userName);
        if (user != null && user.getPassword().equals(password)) {
            result.put("code", 200);
            result.put("message", "登录成功");
            result.put("user", user);
            // 单独返回用户ID，方便前端直接获取
            result.put("user_id", user.getUserId());
            // 单独返回用户角色，方便前端直接获取
            result.put("user_role", user.getUserRole());
        } else {
            result.put("code", 401);
            result.put("message", "用户名或密码错误");
        }
        return result;
    }

}